﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.Windows.Threading;
using Budgetsch.ViewModels.Statistics;

namespace Budgetsch.Views.Statistics
{
    public partial class StatisticsView : Page
    {
        private StatisticsVM m_vm;
        public StatisticsView()
        {
            m_vm = new StatisticsVM();
            this.DataContext = m_vm;
            InitializeComponent();
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            m_vm.PropertyChanged += (o, ee) =>
            {
                if (ee.PropertyName == "IsInMonthMode")
                {
                    FillStackedChart();
                }
            };
            if (m_vm.IsStackReady)
            {
                FillStackedChart();
            }
            else
            {
                var dt = new DispatcherTimer();
                dt.Interval = new TimeSpan(0, 0, 0, 0, 500);
                dt.Tick += (oo, eee) =>
                {
                    if (m_vm.IsStackReady)
                    {
                        ((DispatcherTimer)oo).Stop();
                        FillStackedChart();
                    }
                    x.Text = eee.GetHashCode().ToString();
                };
                dt.Start();
            }
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void FillStackedChart()
        {
            var stackedseries = new StackedColumnSeries();
            var definitions = m_vm.GetStackedChartInfo();
            foreach (var definition in definitions)
            {
                stackedseries.SeriesDefinitions.Add(definition);
            }
            StackedChart.Series.Clear();
            StackedChart.Series.Add(stackedseries);
        }
    }
}
